package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

public class AddFriendValidationActivity extends BaseActivity implements
		OnClickListener {
	private ImageView addfriendvalidation_btn;
	private Button addfriendvalidation_index;
	private EditText addfriend_message_content;
	private String friend_id = null;
	private Results results;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(AddFriendValidationActivity.this);
				Utils.showMessage(AddFriendValidationActivity.this,
						getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(AddFriendValidationActivity.this);
				try {
					if(results.isRet()==true)
					{
						Utils.showMessage(AddFriendValidationActivity.this,
								""+getResources().getString(R.string.l_xb55));
						AddFriendValidationActivity.this.finish();
					}
					else
					{
						Utils.showMessage(AddFriendValidationActivity.this,
								""+getResources().getString(R.string.TheOperationFailure));
						AddFriendValidationActivity.this.finish();
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setFullScreen();
		setContentView(R.layout.addfriendvalidation_interface);
		init();
	}

	private void init() {
		addfriendvalidation_btn = (ImageView) findViewById(R.id.addfriendvalidation_btn);
		addfriendvalidation_index = (Button) findViewById(R.id.addfriendvalidation_index);
		addfriend_message_content = (EditText) findViewById(R.id.addfriend_message_content);
		addfriendvalidation_btn.setOnClickListener(this);
		addfriendvalidation_index.setOnClickListener(this);
		friend_id = getIntent().getStringExtra("id");
	}

	private void Send_Yanzheng() {
		Utils.showPrgress(AddFriendValidationActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(AddFriendValidationActivity.this));
				map.put("fid", friend_id);
				map.put("content", addfriend_message_content.getText()
						.toString().trim());
				String addfcontent = Utils.sendRequest(map, Constants.BadiUrl
						+ "add_firend");
				if (addfcontent != null) {
//					   ("加为好友结果是:" + addfcontent);
					results = Utils.checkResult_NNN(getApplicationContext(),
							addfcontent);
					if (results != null && !results.getRetmsg().equals("null")) {
						handler.sendEmptyMessage(2);
					} else {
//						   ("无法解析");
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.addfriendvalidation_btn:
			AddFriendValidationActivity.this.finish();
			break;
		case R.id.addfriendvalidation_index:
			Send_Yanzheng();
			break;

		default:
			break;
		}
	}
}
